package com.ruoyi.gpt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.gpt.domain.bo.GptGoodsBo;
import com.ruoyi.gpt.domain.vo.GptGoodsVo;
import com.ruoyi.gpt.domain.GptGoods;
import com.ruoyi.gpt.mapper.GptGoodsMapper;
import com.ruoyi.gpt.service.IGptGoodsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * Gpt商品列表Service业务层处理
 *
 * @author ztx
 * @date 2023-04-23
 */
@RequiredArgsConstructor
@Service
public class GptGoodsServiceImpl implements IGptGoodsService {

    private final GptGoodsMapper baseMapper;

    /**
     * 查询Gpt商品列表
     */
    @Override
    public GptGoodsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询Gpt商品列表列表
     */
    @Override
    public TableDataInfo<GptGoodsVo> queryPageList(GptGoodsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GptGoods> lqw = buildQueryWrapper(bo);
        Page<GptGoodsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<GptGoodsVo> queryListForApp(GptGoodsBo bo, PageQuery pageQuery) {
        bo.setShelf("1"); // 只查询上架的产品
        LambdaQueryWrapper<GptGoods> lqw = buildQueryWrapperForApp(bo);
        Page<GptGoodsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询Gpt商品列表列表
     */
    @Override
    public List<GptGoodsVo> queryList(GptGoodsBo bo) {
        LambdaQueryWrapper<GptGoods> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GptGoods> buildQueryWrapper(GptGoodsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GptGoods> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getGoodsName()), GptGoods::getGoodsName, bo.getGoodsName());
        lqw.eq(StringUtils.isNotBlank(bo.getShelf()), GptGoods::getShelf, bo.getShelf());
        lqw.eq(StringUtils.isNotBlank(bo.getGoodsType()), GptGoods::getGoodsType, bo.getGoodsType());
        lqw.eq(StringUtils.isNotBlank(bo.getAppid()), GptGoods::getAppid, bo.getAppid());
        return lqw;
    }
    private LambdaQueryWrapper<GptGoods> buildQueryWrapperForApp(GptGoodsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GptGoods> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getGoodsName()), GptGoods::getGoodsName, bo.getGoodsName());
        lqw.eq(GptGoods::getShelf, bo.getShelf());
        lqw.eq(StringUtils.isNotBlank(bo.getGoodsType()), GptGoods::getGoodsType, bo.getGoodsType());
        lqw.eq(GptGoods::getAppid, bo.getAppid());
        // 根据sort字段排序
        lqw.orderByAsc(GptGoods::getSort);
        return lqw;
    }

    /**
     * 新增Gpt商品列表
     */
    @Override
    public Boolean insertByBo(GptGoodsBo bo) {
        GptGoods add = BeanUtil.toBean(bo, GptGoods.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改Gpt商品列表
     */
    @Override
    public Boolean updateByBo(GptGoodsBo bo) {
        GptGoods update = BeanUtil.toBean(bo, GptGoods.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GptGoods entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除Gpt商品列表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
